草庐IT

HTTP 服务器

全部标签

unit-testing - 尝试在 TCP 监听器上提供服务时,由于超时,Go 测试模棱两可地失败

我有这个单元测试:funcTestServer(t*testing.T){db:=prepareDBConn(t)deferdb.Close()lis:=bufconn.Listen(1024*1024)t.Logf("Openedlistener:%v",lis)grpcServer:=grpc.NewServer(withUnaryInterceptor(),)t.Logf("Openedgrpcserver:%v",grpcServer)signKey:=getSignKey()ifsignKey==nil{t.Fatal("FailedtofindorparseRSApriva

Golang http写响应无需等待完成

我正在构建一个应用程序,它构建一个pdf文件并在收到请求时将其返回给客户端。由于其中一些pdf文件可能需要一些时间才能生成,我想在客户端运行时定期将某种状态更新发送回客户端。当它完成构建pdf文件时,它也应该返回给客户。类似于:funcbuildReport(writerhttp.ResponseWriter,request*http.Request){//buildpdfbuildpdffilefor{//forexamplepurposesonlywriter.Write([]byte("building.Pleasewait."))}pdf.OutputFileAndClose(

go - HTTP.Server 关闭导致运行时错误

我有一个在darwin/amd64上用Go1.9.2编写的goroutine,它会导致运行时错误:无效的内存地址或nil指针取消引用。我认为这是因为某种与例程退出顺序相关的竞争条件,但我不确定。主应用程序正在做几件事,所以我将网络服务器作为goroutine启动,然后监听来自父进程的退出信号并尝试在返回之前彻底关闭所有内容。函数如下://WebServerdefinesthehandlerendpointsandlaunchesthewebserverlistenerfuncWebServer(wg*sync.WaitGroup){//Makesuretheexitisnoteddef

Golang IPv6 服务器

如何创建ipv6服务器。ipv4服务器看起来像packagemainimport("fmt""net/http")funch(whttp.ResponseWriter,r*http.Request){fmt.Fprintf(w,"Test")}funcmain(){http.HandleFunc("/",h)http.ListenAndServe(":80",nil)}但是如何在80端口监听ipv6 最佳答案 它已经在监听ipv6(以及ipv4)。funcListenAndServe(addrstring,handlerHandle

http - 在 Golang 中处理并发 HTTP 请求

我正在尝试处理一个包含200个URL的文件,并使用每个URL发出一个HTTP请求。我每次最多需要同时处理10个URL(代码应该阻塞,直到10个URL完成处理)。试图在go中解决它,但我一直在处理整个文件,并创建了200个并发连接。forscanner.Scan(){//loopthrougheachurlinthefile//sendeachurltogolangHTTPrequestgoHTTPrequest(scanner.Text(),channel,&wg)}fmt.Println(我该怎么办? 最佳答案 从channel中

http - 戈兰 http.Get()

我正在尝试编写一个基本的http服务器示例。我可以curllocalhost:8080,但无法通过客户端脚本使用http.Get("127.0.0.1:8080")联系服务器。我做错了什么?server.go:import"fmt"import"net/http"funcjoin(whttp.ResponseWriter,req*http.Request){fmt.Println("Someonejoined")}funcmain(){fmt.Println("Listeningonport8080...")http.HandleFunc("/join",join)http.Liste

http - 如何倒带 io.ReadCloser

我总是被io.ReadCloser困住,然后忘记我以前读过它,当我再次阅读它时,我得到一个空的负载。我希望对我的愚蠢进行一些lint检查。尽管如此,我认为我可以使用TeeReader,但它在这里没有达到我的期望:funcmain(){http.HandleFunc("/",func(whttp.ResponseWriter,r*http.Request){buf:=&bytes.Buffer{}tee:=io.TeeReader(r.Body,buf)body,err:=ioutil.ReadAll(tee)iferr!=nil{http.Error(w,err.Error(),htt

bash - 在 Docker 容器中启动 Golang Web 服务器

我正在创建一项服务,除其他外,该服务允许用户随意启动/停止Golang网络服务器。为了让Nodejs服务器在类似情况下运行,我只需在容器启动时运行的批处理文件中发出nodejs/path/to/index.js&disown即可。基本位ADDgorun.sh/usr/local/bin/gorun.shRUNchmod+x/usr/local/bin/gorun.sh...ENTRYPOINT["/bin/bash"]CMD["/usr/local/bin/gorun.sh"]这每次都完美无缺。在gorun.sh我有nodejs/path/to/index.js&disown行。既然我

Go 服务器在 arm64 上返回损坏的图像

我写了一个简单的HTTP图像服务器:go/src/demo/demo.go:packagemainimport("net/http""github.com/gorilla/mux")funcmain(){router:=mux.NewRouter()router.HandleFunc("/foobar",func(whttp.ResponseWriter,r*http.Request){http.ServeFile(w,r,"/home/foobar/test.jpg")})http.ListenAndServe(":5000",router)}我编译它(PWD=$HOME/go/sr

go - 如何为 REST 服务生成代码覆盖率

我想获得用Go编写的REST服务的测试范围。我通过goroutine生成REST服务,然后使用rest客户端发出HTTP请求,并查看HTTP响应。测试成功通过,但gotest-cover返回0%的测试覆盖率。有没有办法获得golangREST服务中使用的所有包的实际测试覆盖率。我的测试文件:main_test.goimport("testing")//Teststartedwhenthetestbinaryisstarted.Onlycallsmain.funcTestSystem(t*testing.T){gomain()//SpinningupthegolangRESTserver